﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using PSE.Deployment.SetupBase.Configuration.Enums;

namespace PSE.Deployment.SetupBase.Configuration
{
    [Serializable]
    public class Module
    {
        [XmlAttribute()]
        public string Id { get; set; }
        [XmlAttribute()]
        public string Name { get; set; }
    }

    [Serializable]
    public class Group
    {
        [XmlAttribute()]
        public string Id { get; set; }
        [XmlAttribute()]
        public string Name { get; set; }
        [XmlAttribute()]
        public string Description { get; set; }
        [XmlAttribute()]
        public int Index { get; set; }
        [XmlElement(IsNullable = true)]
        public bool? Install { get; set; }
        [XmlAttribute()]
        public bool Installed { get; set; }
        public Module Module { get; set; }        
        public List<Layer> Layers { get; set; }
        public SystemInfo System { get; set; }
        public Group ParentGroup { get; set; }
        public List<Group> SubGroups { get; set; }
        public List<ActionBase> Actions { get; set; }
        public List<Group> Dependencies { get; set; }

        public Group()
        {
            Install = false;
            SubGroups = new List<Group>();
            Actions = new List<ActionBase>();
            Dependencies = new List<Group>();
            System = new SystemInfo();
            Layers = new List<Layer>();
            Module = new Module();
        }

        public void AddSubGroup(Group subGroup)
        {
            Group group = subGroup.Clone();
            group.ParentGroup = new Group() { Id = this.Id };

            this.SubGroups.Add(group);
        }

        public void AddDependency(Group dependency)
        {
            Group group = dependency.Clone();
            group.ParentGroup = new Group() { Id = this.Id };

            this.Dependencies.Add(group);
        }

        public Group Clone()
        {
            Group g = new Group();
            g.Actions = this.Actions;

            foreach (Group item in this.Dependencies)
            {
                g.Dependencies.Add(item.Clone());
            }

            foreach (Group item in this.SubGroups)
            {
                g.SubGroups.Add(item.Clone());
            }

            g.Description = this.Description;
            g.Id = this.Id;
            g.Index = this.Index;
            g.Install = this.Install;
            g.Installed = this.Installed;
            g.Name = this.Name;
            g.Layers = this.Layers;
            g.Module = this.Module;
            
            if (this.ParentGroup != null)
                g.ParentGroup = this.ParentGroup.Clone(); //new Group() { Id = this.Id };

            g.System = this.System;
            return g;
        }
    }
}
